package com.m.service.impl;

import com.m.config.SceneProperty;
import com.m.dao.entity.SceneManagerEntity;
import com.m.dao.mapper.SceneManagerMapper;
import com.m.enums.SceneType;
import com.m.exception.RiskControllerException;
import com.m.pojo.dto.scenemanager.AddSceneInfoDto;
import com.m.pojo.dto.scenemanager.SceneDimensionDto;
import com.m.pojo.dto.scenemanager.SceneInfoDto;
import com.m.pojo.dto.scenemanager.SceneSearchDto;
import com.m.service.ISceneService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 功能：<br/>
 *
 * @author miracle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class SceneServiceImpl implements ISceneService {
    private static final Logger logger = LoggerFactory.getLogger(SceneServiceImpl.class);

    @Resource
    private SceneManagerMapper sceneManagerMapper;
    @Resource
    private SceneProperty sceneProperty;

    @Override
    public void addScene(AddSceneInfoDto sceneInfoDto, MultipartFile file) {
        // 1.校验
        if (sceneInfoDto.getName() == null || "".equals(sceneInfoDto.getName())
                || !SceneType.getAllSceneTypeIds().contains(sceneInfoDto.getType())) {
            throw new RuntimeException("参数异常");
        }

        File dstFile = new File(sceneProperty.getTmp(), System.currentTimeMillis() + "_" + file.getOriginalFilename());
        try {
            file.transferTo(dstFile);
        } catch (IOException e) {
            throw new RuntimeException("附件保存失败");
        }
        SceneManagerEntity sceneManagerEntity = new SceneManagerEntity();
        sceneManagerEntity.setName(sceneInfoDto.getName());
        sceneManagerEntity.setType(sceneInfoDto.getType());
        sceneManagerEntity.setJarPath(dstFile.getAbsolutePath());
        sceneManagerMapper.insertUseGeneratedKeys(sceneManagerEntity);
    }

    @Override
    public void delScene(Long sceneId) {
        // 校验ID是否存在
        SceneManagerEntity sceneManagerEntity = sceneManagerMapper.selectByPrimaryKey(sceneId);
        if (sceneManagerEntity == null) {
            throw new RiskControllerException("id对应的信息不存在");
        }

        sceneManagerMapper.deleteByPrimaryKey(sceneId);
    }

    @Override
    public List<SceneInfoDto> getSceneInfo(SceneSearchDto sceneSearchDto) {
        List<SceneInfoDto> allSceneInfos = sceneManagerMapper.getAllSceneInfo(sceneSearchDto.getName(), sceneSearchDto.getType());
        Map<Integer, String> allSceneTypeMaps = SceneType.getAllSceneTypeMaps();
        if (allSceneInfos != null) {
            allSceneInfos.forEach(sceneInfoDto -> {
                sceneInfoDto.setTypeName(allSceneTypeMaps.get(sceneInfoDto.getType()));
            });
        }
        return allSceneInfos;
    }

    @Override
    public List<SceneDimensionDto> getSceneDimInfo(Long sceneId) {
        return sceneManagerMapper.getSceneDimInfo(sceneId);
    }
}
